package com.colin.app.activity.senior;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import com.colin.app.app.BaseModelImpl;
import com.colin.app.bean.DemoBean;
import com.colin.app.bean.ItemBean;
import com.colin.library.other.Constants;
import com.colin.library.other.Url;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Colin on 2017/2/7.
 */

public class SeniorModelImpl extends BaseModelImpl implements SeniorContract.SeniorModel {
    private DemoBean demoBean;
    private int position = 0 ;

    public SeniorModelImpl(DemoBean demoBean) {
        this.demoBean = demoBean;
    }

    @Override
    public String getTitle() {
        String title = "Title";
        if (null != demoBean) {
            title = demoBean.getTitle();
        }
        return title;
    }


    @Override
    public Animation getAlphaAnimation() {
        //实例化 AlphaAnimation 主要是改变透明度
        //透明度 从 1-不透明 0-完全透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        //设置动画插值器 被用来修饰动画效果,定义动画的变化率
        alphaAnimation.setInterpolator(new DecelerateInterpolator());
        //设置动画执行时间
        alphaAnimation.setDuration(Constants.ANIMATION_DURATION_TIME);
        return alphaAnimation;
    }

    @Override
    public Animation getRotateAnimation() {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 100, 100);
        rotateAnimation.setDuration(Constants.ANIMATION_DURATION_TIME);
        return rotateAnimation;
    }

    @Override
    public Animation getRotateAnimationSelf() {
        //实例化RotateAnimation
        //以自身中心为圆心，旋转360度  正值为顺时针旋转，负值为逆时针旋转
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //设置动画插值器 被用来修饰动画效果,定义动画的变化率
        rotateAnimation.setInterpolator(new DecelerateInterpolator());
        //设置动画执行时间
        rotateAnimation.setDuration(Constants.ANIMATION_DURATION_TIME);
        return rotateAnimation;
    }

    @Override
    public Animation getTranslateAnimation() {
        //以自身为坐标系和长度单位，从(0,0)移动到(1,1)
        Animation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f);
        //设置动画插值器 被用来修饰动画效果,定义动画的变化率
        translateAnimation.setInterpolator(new DecelerateInterpolator());
        //设置动画执行时间
        translateAnimation.setDuration(Constants.ANIMATION_DURATION_TIME);
        return translateAnimation;
    }

    @Override
    public Animation getScaleAnimation() {
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 2, 0, 2);
        scaleAnimation.setDuration(Constants.ANIMATION_DURATION_TIME);
        return scaleAnimation;
    }

    @Override
    public Animation getScaleAnimationSelf() {
        //实例化 ScaleAnimation 主要是缩放效果
        //参数：fromX-动画开始前，x坐标   toX-动画结束后x坐标
        //fromY-动画开始前，Y坐标  toY-动画结束后Y坐标
        //pivotXType - 为动画相对于物件的X坐标的参照物   pivotXValue - 值
        //pivotYType - 为动画相对于物件的Y坐标的参照物   pivotYValue - 值
        //ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
        Animation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //设置动画插值器 被用来修饰动画效果,定义动画的变化率
        scaleAnimation.setInterpolator(new DecelerateInterpolator());
        //设置动画执行时间
        scaleAnimation.setDuration(Constants.ANIMATION_DURATION_TIME);
        return scaleAnimation;
    }

    @Override
    public Animation getAnimationSet() {
        //AnimationSet类是一个Animation集合，里面可以许多Animation，
        //且在AnimationSet中设置的属性适用于里面的所有Animation。
        //参数true 则共享@Interpolator
        AnimationSet animationSet = new AnimationSet(true);
        //透明
        Animation alphaAnim = getAlphaAnimation();
        animationSet.addAnimation(alphaAnim);
        //缩放
        Animation scaleAnimation = getScaleAnimation();
        animationSet.addAnimation(scaleAnimation);
        //旋转
        Animation rotateAnim = getRotateAnimation();
        animationSet.addAnimation(rotateAnim);
        //移动 上面三个动画是同时进行的，我现在需要让移动这个动画在上面的动画之后执行
        //需要使用setStartOffset 设置动画开始的时间
        Animation translateAnim = getTranslateAnimation();
        translateAnim.setStartOffset(500);
        animationSet.addAnimation(translateAnim);

        TranslateAnimation translateAnimation = new TranslateAnimation(0, -100, 0, -200);
        translateAnimation.setDuration(Constants.ANIMATION_DURATION_TIME);
        return translateAnimation;
    }

    @Override
    public List<PropertyValuesHolder> getAnimationPropertyValuesHolderList() {
        PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("translationX", 0, 300f, 0);
        PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofFloat("scaleX", 0, 1f, 0, 1f);
        PropertyValuesHolder propertyValuesHolder3 = PropertyValuesHolder.ofFloat("scaleY", 0, 1f, 0, 1f);
        List<PropertyValuesHolder> propertyValuesHolderList = new ArrayList<>();
        propertyValuesHolderList.add(propertyValuesHolder1);
        propertyValuesHolderList.add(propertyValuesHolder2);
        propertyValuesHolderList.add(propertyValuesHolder3);
        return propertyValuesHolderList;
    }

    @Override
    public AnimatorSet getAnimationSetCopyHolder(View view) {
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "translationX", 0, 300f, 0);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "scaleX", 0, 1f, 0f, 1f);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "scaleY", 0, 1f, 0f, 1f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(Constants.ANIMATION_DURATION_TIME);
        animatorSet.playTogether(animator1, animator2, animator3);
        return animatorSet;
    }

    @Override
    public ValueAnimator getValuesAnimation() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 100);
        valueAnimator.setDuration(Constants.ANIMATION_DURATION_TIME);
        return valueAnimator;
    }

    @Override
    public List<ItemBean> getItemBeanList(int page) {
        List<ItemBean> itemBeanList = new ArrayList<>();
        ItemBean itemBean = null;
        for (int i = 0; i < pageSize; i++) {
            itemBean = new ItemBean().setImage(Url.image_url[i])
                    .setImageLarge(Url.image_url[i])
                    .setAuthor_intro("Colin")
                    .setTitle("Colin-->>" + String.valueOf((page - 1) * pageSize + i + 1))
                    .setCatalog("Colin");
            itemBeanList.add(itemBean);
        }
        return itemBeanList;
    }

    @Override
    public void setPullRefreshViewChild(int position) {
        this.position = position;
    }

    @Override
    public int getPullRefreshViewChild() {
        return position;
    }

}
